package com.envper.security.utils;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Set;

public class PermUtils {

    public static boolean verify(String requestURI, Set<String> permissions) {
        if (StringUtils.hasText(requestURI) && !CollectionUtils.isEmpty(permissions)) {
            String replace = requestURI.substring(1).replace("/", ":");
            String[] split = replace.split(":");
            for (String perms : permissions) {
                if (perms == null) continue; // 检查 perms 是否为 null
                int status = 0;
                String[] perm = perms.split(":");
                for (int p = 0; p < perm.length; p++) {
                    if (split.length <= p) break;
                    if (split[p].equals(perm[p]) || perm[p].equals("**")) status++;
                }
                if (status == perm.length) return true;
            }
        }
        return false;
    }
}